<template>
    <div id="virtualReality">
        <div class="image-container">
            <div class="image-content" ref="image-content"></div>
        </div>
        <div class="bottom-handle flex-between">
            <div class="name-container flex-start">
                <div class="image-name pointer" :class="{'active-name':actived===index}" v-for="(item, index) in vrList" :key="index" @click="changeImage(index)">
                    {{item.name}}
                </div>
            </div>
            <div class="page-change-arrows flex-start">
                <div class="change page-left flex-center pointer" @click="prevImg">
                    <img src="../../assets/img/page-left.png" alt="加载失败">
                </div>
                <div class="change page-right flex-center pointer" @click="nextImg">
                    <img src="../../assets/img/page-right.png" alt="加载失败">
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import mixin from '../../mixin/mixin'
import PhotoSphereViewer  from 'photo-sphere-viewer';
import 'photo-sphere-viewer/dist/photo-sphere-viewer.css'
export default {
    name: "VirtualReality",
    mixins: [mixin],
    data() {
        return {
            vrList: [],
            actived: 0,
            vrImage: null,
        }
    },
    mounted() {
        if (this.bsr.images) {
            this.bsr.images.forEach(v => {
                let suffix = this.getFileSuffix(v.image)
                let obj = {}
                obj.image = window.$DATADIR + `resc/virtualReality/${this.bsr.id}/${v.image.replace(/\.(?=[a-z])[a-z]{2,3}[a-z0-9]$/i, suffix)}`
                obj.name = v.name
                this.vrList.push(obj)
            })
        }
        this.$nextTick(() => {
            this.initImage()
        })
    },
    methods: {
        // setPanorama 
        initImage() {
            this.vrImage = PhotoSphereViewer({
                container: this.$refs['image-content'],
                panorama: this.vrList[this.actived].image,
                time_anim:false,
                navbar: [
                    'autorotate',
                    'zoom',
                    
                ],

            })
        },
        changeImage(index) {
            this.actived = index
            if (this.vrImage) {
                this.vrImage.destroy()
            }
            this.initImage()
        },
        prevImg() {
            if (this.actived <= 0) return
            --this.actived
            if (this.vrImage) {
                this.vrImage.destroy()
            }
            this.initImage()
        },
        nextImg() {
            if (this.actived >= this.vrList.length - 1) return
            ++this.actived
            if (this.vrImage) {
                this.vrImage.destroy()
            }
            this.initImage()
        }
    }
}
</script>

<style lang="scss">
#virtualReality {
    .image-content {
        width: 100vw;
        height: calc(100vh - 80px);
    }
    .name-container {
        flex: 1;

        .image-name {
            min-width: 80px;
            height: 32px;
            line-height: 32px;
            text-align: center;
            border: 1px solid rgba(72, 148, 255, 1);
            color: rgba(72, 148, 255, 1);
            border-radius: 16px;
            margin-right: 14px;
        }
        .active-name {
            background: rgba(72, 148, 255, 1);
            color: #fff;
        }
    }
    .bottom-handle {
        max-width: 100vw;
        height: 50px;
        background: #dcdfe7;
        padding: 0 20px;
    }

    .page-change-arrows {
        width: 64px;
    }
    .change {
        width: 32px;
        height: 32px;
        background: rgba(220, 223, 231, 1);
        border: 1px solid rgba(72, 148, 255, 1);
        img {
            width: 11px;
            height: 11px;
        }
    }

    .page-left {
        border-radius: 8px 0px 0px 8px;
    }
    .page-right {
        border-radius: 0px 8px 8px 0px;
        border-left: 1px solid rgba(72, 148, 255, 1);
    }
}
</style>
